package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.ShopTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1ItemStatus;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1TaxCalculator;
import com.lineage.server.model.shop.L1Shop;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.templates.L1ShopItem;
import com.lineage.server.world.World;
import java.io.IOException;
import java.util.List;

public class S_ScrollShopSellList extends ServerBasePacket {
  public S_ScrollShopSellList(L1Npc npc) {
    writeC(70);
    int objId = World.get().getObjId(npc);
    L1Object npcObj = World.get().findObject(objId);
    writeD(objId);
    int npcId = ((L1NpcInstance)npcObj).getNpcTemplate().get_npcId();
    L1TaxCalculator calc = new L1TaxCalculator(npcId);
    L1Shop shop = ShopTable.get().get(npcId);
    List<L1ShopItem> shopItems = shop.getSellingItems();
    writeH(shopItems.size());
    L1ItemInstance dummy = new L1ItemInstance();
    int i = 0;
    while (i < shopItems.size()) {
      L1ShopItem shopItem = shopItems.get(i);
      L1Item item = shopItem.getItem();
      int price = calc.layTax(shopItem.getPrice());
      writeD(i);
      writeH(shopItem.getItem().getGfxId());
      writeD(price);
      if (shopItem.getPackCount() > 1) {
        writeS(String.valueOf(String.valueOf(item.getName())) + " (" + shopItem.getPackCount() + ")");
      } else {
        writeS(item.getName());
      } 
      L1Item template = ItemTable.get().getTemplate(item.getItemId());
      if (template == null) {
        writeC(0);
      } else {
        L1ItemStatus itemInfo = new L1ItemStatus(item);
        byte[] status = itemInfo.getStatusBytes(true).getBytes();
        writeC(status.length);
        byte[] array;
        int length = (array = status).length;
        int j = 0;
        while (j < length) {
          byte b = array[j];
          writeC(b);
          j++;
        } 
      } 
      i++;
    } 
    writeH(7);
  }
  
  public byte[] getContent() throws IOException {
    return this._bao.toByteArray();
  }
}
